package com.zfy.cmsservice.service.impl;

import com.zfy.cmsservice.entity.Courses;
import com.zfy.cmsservice.entity.ExamScores;
import com.zfy.cmsservice.entity.Students;
import com.zfy.cmsservice.entity.excel.ExamScoreDTO;
import com.zfy.cmsservice.entity.result.ScoreStatistic;
import com.zfy.cmsservice.entity.vo.ExamScoreVo;
import com.zfy.cmsservice.entity.vo.ScoresVo;
import com.zfy.cmsservice.mapper.CoursesMapper;
import com.zfy.cmsservice.mapper.ExamScoresMapper;
import com.zfy.cmsservice.mapper.StudentsMapper;
import com.zfy.cmsservice.service.ExamScoresService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zfy.cmsservice.service.StudentsService;
import com.zfy.cmsservice.utils.ScoreUtils;
import com.zfy.commonutils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 考试成绩表 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2023-12-10
 */
@Service
public class ExamScoresServiceImpl extends ServiceImpl<ExamScoresMapper, ExamScores> implements ExamScoresService {
    @Autowired
    private ExamScoresMapper examScoresMapper;
    @Autowired
    private StudentsMapper studentsMapper;
    @Autowired
    private CoursesMapper coursesMapper;

    @Override
    public R pageExamScores(long page, long size, Integer begin, Integer end) {
        page = (page-1)*size;
        List<ScoresVo> records = examScoresMapper.pageExamScores(page,size,begin,end);
        int total = examScoresMapper.pageExamScoresTotal(page,size,begin,end);
        return R.ok().data("total", total).data("content", records);
    }

    @Override
    public R getExamScores(String idNumber, String semesterId) {
        List<ExamScoreVo> content = examScoresMapper.getExamScores(idNumber,semesterId);
        for (ExamScoreVo examScore : content) {
            if(examScore.getScore() != null){
                examScore.setLevel(ScoreUtils.evaluateScore(examScore.getScore()));
            }
        }
        return R.ok().data("content", content);
    }

    @Override
    public List<ScoreStatistic> getScoreStatistics() {
        return examScoresMapper.getScoreStatistics();
    }

    @Override
    public List<ExamScoreDTO> getExamScoreStatistics(String semestersId, String classesId) {
        List<ExamScoreDTO> result = new ArrayList<>();

        // 获取特定年级和班级的所有学生
        List<Students> students = studentsMapper.getStudentsName(classesId,null);

        for (Students student : students) {
            // 获取该学生的所有考试成绩
            List<ExamScores> examScores = examScoresMapper.findByStudentId(student.getId());

            for (ExamScores score : examScores) {

                // 构建DTO对象并添加到结果列表中
                ExamScoreDTO dto = new ExamScoreDTO();
                dto.setStudentName(student.getName());
                dto.setScore(score.getScore());
                result.add(dto);
            }
        }

        return result;
    }

}
